package com.hxkj.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxkj.client.service.ArticleServiceClient;
import com.hxkj.dao.SiteDao;
import com.hxkj.dao.UserArticleDao;
import com.hxkj.dao.WebsiteAddrDao;
import com.hxkj.domain.po.Article;
import com.hxkj.domain.po.Site;
import com.hxkj.domain.po.UserArticle;
import com.hxkj.domain.po.WebsiteAddr;
import com.hxkj.service.ArticleCollectionService;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.to.PageTo;
import com.platform.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 文章收藏
 * created by chenfangzhou on 2018/8/13
 */
@Service
@Slf4j
public class ArticleCollectionServiceImpl implements ArticleCollectionService {

    @Autowired
    private UserArticleDao userArticleDao;
    @Autowired
    private SiteDao siteDao;
    @Autowired
    private ArticleServiceClient articleServiceClient;
    @Autowired
    private WebsiteAddrDao websiteAddrDao;

    /**
     * 收藏文章
     */
    @Override
    public ResultMessageStruct collectArticle(UserArticle userArticle) throws Exception {
        ResultMessageStruct result;
        try {
            result = new ResultMessageStruct();
            Site site = siteDao.get(userArticle.getSiteId());
            if(site != null){
                userArticle.setSiteName(site.getSiteName());
            }
            // 防止同一篇文章重复收藏
            userArticle.setDelTag(0);
            List<UserArticle> list = userArticleDao.get(userArticle);
            if (list.size() > 0) {
                result.setCode(301);
                result.setMessage("此文章已在收藏列表");
                return result;
            }
            userArticle.setCreateTime(System.currentTimeMillis());
            userArticle.setUpdateTime(System.currentTimeMillis());
            int i = userArticleDao.save(userArticle);
            result.setCode(i == 1 ? 200 : 401);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    /**
     * 取消收藏文章
     */
    @Override
    public ResultMessageStruct cancelCollectArticle(UserArticle userArticle) {
        ResultMessageStruct result = new ResultMessageStruct();
        try {
            //需要提供用户编号和文章编号
            userArticleDao.delete(userArticle);
            result.setCode(200);
            result.setMessage("取消收藏文章失败");
        } catch (Exception e) {
            result.setCode(303);
            result.setMessage("取消收藏文章失败");
        }
        return result;
    }

    @Override
    public int ifCollectArticle(UserArticle userArticle) throws Exception{
        return userArticleDao.ifCollectArticle(userArticle);
    }

    /**
     * 获取收藏文章列表
     */
    @Override
    public PageInfo<UserArticle> getCollectionList(UserArticle userArticle, PageTo pageParam) throws Exception {
        try {
            Page<UserArticle> page = PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            userArticleDao.get(userArticle);
            PageInfo<UserArticle> pageInfo = new PageInfo<>(page);
            for (UserArticle article : pageInfo.getList()) {
                ResultDataStruct<Article> result = articleServiceClient.getArticleByArticleId(article.getArticleId());
                WebsiteAddr param = new WebsiteAddr();
                param.setSiteId(article.getSiteId());
                List<WebsiteAddr> entity = websiteAddrDao.get(param);
                String ip = "";
                if(entity != null && entity.size()>0){
                    if(StringUtils.isNotBlank(entity.get(0).getIpAdd())){
                        ip = entity.get(0).getIpAdd();
                    }
                }
                if (200 != result.getCode())
                    log.info(result.getMessage());
                if (result.getData() != null) {
                    article.setUrl("http://"+ip+result.getData().getManuscriptPath());
                    article.setTitle(result.getData().getHeadline());
                }
            }

            return pageInfo;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
}
